package com.pizidea.imagepicker.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.mobilecomps.R;
import com.efounder.utils.ResStringUtil;

import static com.pizidea.imagepicker.ui.ImagePreviewFragment.mContext;


public class LoadingDataUtilBlack {

    /*
     * 定义loadingDialog
     */
    private static Dialog loadingDialog;

    /*
     * 定义Appcontext全局变量
     */
    //public static AppContext appContext = AppContext.getInstance();
    /*
     * 加载显示的文字
     */
    public static String stringShow;

    /*
     * 重载方法
     */
    public static void show(Context context) {

        if (!((Activity) context).isFinishing())
            show(mContext, ResStringUtil.getString(R.string.common_text_loading));
    }

    /*
     * 显示dialog
     */
    public static void show(Context context, String msg) {
        if (loadingDialog == null || !loadingDialog.isShowing()) {

            // 用来载入LinearLayout
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            // 创建视图R.layout.loadingdata_dialog_ios
            View v = layoutInflater.inflate(R.layout.loadingdata_dialog_ios, null);
            // 加载布局
            LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view_ios);
            // 加载imageView
            ImageView imageView = (ImageView) v.findViewById(R.id.loading_img_ios);
            ImageView imageViewa = (ImageView) v.findViewById(R.id.loading_img_iosa);
            // 设置textVeiw
            TextView textView = (TextView) v.findViewById(R.id.loading_textView_ios);
            if (msg.equals("")) {
                textView.setText(msg);
            } else {
                //textView.setText(msg+"..." );
                textView.setText(msg);
            }
            Resources res = context.getResources();
            float fontsize = res.getDimension(R.dimen.activity_login_button_fontsize);
//            textView.setTextSize(16);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

            //获取Imageview设置图片的宽度和高度
            Bitmap bitmap = ((BitmapDrawable) imageViewa.getBackground()).getBitmap();
            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
            animationDrawable.start();
            loadingDialog = new Dialog(context, R.style.loadingData_dialog);
            loadingDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            loadingDialog.setContentView(layout);
            //设置dialog的大小
            Window dialogWindow = loadingDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = bitmap.getWidth() + textView.getLineHeight() + 200;
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            textView.measure(w, h);
            lp.height = bitmap.getHeight() + textView.getMeasuredHeight() + 110;
            dialogWindow.setAttributes(lp);
            if (!((Activity) context).isFinishing())
                loadingDialog.show();
        }
    }

	/*
     * 取消dialog
	 */

    public static void dismiss() {
//        if (loadingDialog != null) {
//            loadingDialog.dismiss();
//            loadingDialog=null;
//        }
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                Context context = ((ContextWrapper) loadingDialog.getContext()).getBaseContext();
                if (context instanceof Activity) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
                            loadingDialog.dismiss();
                    }
                } else {
                    loadingDialog.dismiss();
                }
            }
            loadingDialog = null;
        }
    }

	/*
     * 取消dialog
	 */

    public static Dialog getDialog() {
        return loadingDialog;
    }


}
